package com.glory.brepda.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.activity.check.QCDetailActivity;
import com.glory.brepda.entity.QCCheckBean;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class QCCheckAdapter extends BaseQuickAdapter<QCCheckBean.QCFORMDTO, BaseViewHolder> {

    private Activity activity;

    public QCCheckAdapter(@Nullable List<QCCheckBean.QCFORMDTO> data, Activity activity) {
        super(R.layout.adapter_qc_check,data);
        this.activity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, QCCheckBean.QCFORMDTO item) {
        if("CALLEQUIPMENTMAINTENANCE".equals(item.getTASK_SOURCE())||"Equipment_Maintenance".equals(item.getTASK_SOURCE())){
            helper.setText(R.id.tvTaskCategory,"设备叫修");
        }else{
            helper.setText(R.id.tvTaskCategory,"弹坑任务");
        }
        String user_role = item.getUSER_ROLE();

        switch (user_role){
            case "tech_timing":
                helper.setText(R.id.tvRole,"技术员");
                break;
            case "qc_timing":
                helper.setText(R.id.tvRole,"QC");
                break;
            case "op_timing":
                helper.setText(R.id.tvRole,"OP");
                break;
        }


        helper.setText(R.id.tvTriggerTime,item.getCREATED());
        helper.setText(R.id.tvPassTime,calculateHoursDifference(item.getCREATED()));
        helper.setText(R.id.tvLotID,item.getLOT_ID());
        helper.setText(R.id.tvPosition,item.getEQUIPMENT_LOCATION());

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Context context = helper.itemView.getContext();
                Intent intent = new Intent(context, QCDetailActivity.class);
                intent.putExtra("qc_form", item); // 传递数据
                activity.startActivityForResult(intent,1);
            }
        });

    }

    public static String calculateHoursDifference(String createdTimeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            Date createdDate = sdf.parse(createdTimeStr);
            Date now = new Date();
            long diffMillis = now.getTime() - createdDate.getTime();
            double diffHours = diffMillis / (1000.0 * 60 * 60);  // 转换为小时
            return String.format("%.2fH", diffHours);  // 拼接 H
        } catch (ParseException e) {
            e.printStackTrace();
            return "0.00H";
        }
    }

}
